package com.ruoyi.business.controller;

import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.JtDistributorLevel;
import com.ruoyi.business.service.IJtDistributorLevelService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分销商等级Controller
 * 
 * @author Alan Zhang
 * @date 2025-06-17
 */
@Controller
@RequestMapping("/business/distributor/level")
public class JtDistributorLevelController extends BaseController
{
    private String prefix = "business/distributor/level";

    @Autowired
    private IJtDistributorLevelService jtDistributorLevelService;

    @RequiresPermissions("distributor:level:view")
    @GetMapping()
    public String level()
    {
        return prefix + "/level";
    }

    /**
     * 查询分销商等级列表
     */
    @RequiresPermissions("distributor:level:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JtDistributorLevel jtDistributorLevel)
    {
        startPage();
        List<JtDistributorLevel> list = jtDistributorLevelService.selectJtDistributorLevelList(jtDistributorLevel);
        return getDataTable(list);
    }

    /**
     * 导出分销商等级列表
     */
    @RequiresPermissions("distributor:level:export")
    @Log(title = "分销商等级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JtDistributorLevel jtDistributorLevel)
    {
        List<JtDistributorLevel> list = jtDistributorLevelService.selectJtDistributorLevelList(jtDistributorLevel);
        ExcelUtil<JtDistributorLevel> util = new ExcelUtil<JtDistributorLevel>(JtDistributorLevel.class);
        return util.exportExcel(list, "分销商等级数据");
    }

    /**
     * 新增分销商等级
     */
    @RequiresPermissions("distributor:level:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存分销商等级
     */
    @RequiresPermissions("distributor:level:add")
    @Log(title = "分销商等级", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(JtDistributorLevel jtDistributorLevel)
    {
        if(jtDistributorLevel.getOrderAmount() == null &&  jtDistributorLevel.getOrderCount() == null){
            return AjaxResult.error("订单金额和订单数量必须填写一项！");
        }
        JtDistributorLevel param = new JtDistributorLevel();
        param.setLevelName(jtDistributorLevel.getLevelName());

        List<JtDistributorLevel> list = jtDistributorLevelService.selectJtDistributorLevelList(param);
        if(list.size() > 0){
            return AjaxResult.error(jtDistributorLevel.getLevelName() + "已存在！");
        }
        return toAjax(jtDistributorLevelService.insertJtDistributorLevel(jtDistributorLevel));
    }

    /**
     * 修改分销商等级
     */
    @RequiresPermissions("distributor:level:edit")
    @GetMapping("/edit/{levelId}")
    public String edit(@PathVariable("levelId") Long levelId, ModelMap mmap)
    {
        JtDistributorLevel jtDistributorLevel = jtDistributorLevelService.selectJtDistributorLevelByLevelId(levelId);
        mmap.put("jtDistributorLevel", jtDistributorLevel);
        return prefix + "/edit";
    }

    /**
     * 修改保存分销商等级
     */
    @RequiresPermissions("distributor:level:edit")
    @Log(title = "分销商等级", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JtDistributorLevel jtDistributorLevel)
    {
        return toAjax(jtDistributorLevelService.updateJtDistributorLevel(jtDistributorLevel));
    }

    /**
     * 删除分销商等级
     */
    @RequiresPermissions("distributor:level:remove")
    @Log(title = "分销商等级", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jtDistributorLevelService.deleteJtDistributorLevelByLevelIds(ids));
    }
}
